package gdut.jmxywz.action;

import gdut.jmxywz.been.Student;
import gdut.jmxywz.been.Teacher;
import gdut.jmxywz.interceptor.AuthenticationInterceptor;
import gdut.jmxywz.util.ThreeDES;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

public class LoginAction extends BaseAction {
	
	private AuthenticationInterceptor authenticationInterceptor;

	private String userName;
	
	private String password;
	
	private String validateCode;
	
	private Teacher teacher;
	
	private ThreeDES threeDES;

	private Map session;
	
	private String message="";
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getValidateCode() {
		return validateCode;
	}

	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public ThreeDES getThreeDES() {
		return threeDES;
	}

	public void setThreeDES(ThreeDES threeDES) {
		this.threeDES = threeDES;
	}
	
	
	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		message="";
		
		if(this.userName.trim().length()>10){
			message="用户名不能大于10位！请重新输入...";
			return "input";
		}
		if(this.password.trim().length()>50){
			message="密码不能大于50位！请重新输入...";
			return "input";
		}
		if(this.getValidateCode().trim().length()>4){
			message="验证码不能大于4位！请重新输入...";
			return "input";
		}
		
		session=ActionContext.getContext().getSession();
		
		/*从session中取出RandomImageAction.java 中生成的验证码random*/
		String randomIn=(String)session.get("random");
		
		session.put("random", null);
		if(randomIn==null){
			message="验证码为空！请输入验证码...";
			return "input";
		}
		System.out.println("=================程序经过1=================");
		if(randomIn.equals(validateCode)){
			//为了避免直接用数据库中教师表分配给学生用的信息登录，这里直接拦截。
			if(this.getUserName().equals("Student")){
				
				message="用户名和密码不匹配！请重新输入...";
				addActionError("您的用户名和密码不匹配");
				return INPUT;
			}
			
			Teacher teacher=null;
			Student student=null;
			
			threeDES.getKey(threeDES.getTeacherKey());
			String hqlTeacher="select t from Teacher t where t.teacherNum='"+userName+"' and t.teacherPassword='"+threeDES.getEncString(this.getPassword())+"'";
			List teacherList=this.getCommonService().findListByHql(hqlTeacher, null);
			if(teacherList!=null&&teacherList.size()>0){
				teacher=(Teacher)teacherList.get(0);
				session.put("userName", teacher.getTeacherName());
			}
			
			threeDES.getKey(threeDES.getStudentKey());
			String hqlStudent="select s from Student s where s.studentNum='"+userName+"' and s.studentPwd='"+threeDES.getEncString(this.getPassword())+"'";
			List studentList=this.getCommonService().findListByHql(hqlStudent, null);
			if(studentList!=null&&studentList.size()>0){
				student=(Student)studentList.get(0);
				session.put("userName", student.getStudentName());
			}
			if(teacher!=null){
				session.put(authenticationInterceptor.TEACHER_SESSION_KEY, teacher);
				return SUCCESS;	
			}else if(student!=null){ 
				session.put(authenticationInterceptor.STUDENT_SESSION_KEY, student);
				//如果是学生登录，将其转化为教师表的一个帐号，这样便于统一管理学生权限
				String hql="from Teacher t where t.teacherNum='Students'";
				List list=this.getCommonService().findListByHql(hql, null);
				if(list!=null&&list.size()>0){
					teacher=(Teacher)list.get(0);
					session.put("userName", teacher.getTeacherName());
				}
				session.put(authenticationInterceptor.TEACHER_SESSION_KEY, teacher);
				return SUCCESS;	
			}else{
				
				message="用户名和密码不匹配！请重新输入...";
				addActionError("您的用户名和密码不匹配");
				return INPUT;
			}  
		}
		else { 
			message="验证码错误！请输入验证码...";
			return INPUT;   
			}
		
		
	}
	
}
